package utils;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author ���ҷ�
 * @email eliyanfei@126.com	
 * 2017��8��2�� ����2:33:31
 * 
 */
public class JarsLoader {
	public static void loadJars(String path) {
		try {
			File libPath = new File(path);
			File[] jarFiles = libPath.listFiles(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.endsWith("jar");
				}
			});
			if (jarFiles != null) {
				Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
				boolean accessible = method.isAccessible();
				try {
					if (accessible == false) {
						method.setAccessible(true);
					}
					URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
					for (File file : jarFiles) {
						URL url = file.toURI().toURL();
						try {
							method.invoke(classLoader, url);
						} catch (Exception e) {
							System.out.println("jar�����ʧ��");
						}
					}
				} finally {
					method.setAccessible(accessible);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
